﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using ChoViet.Model;

namespace ChoViet.CMS
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "RewriteAdDetails",
                "{clientName}/{cate}/{title}-{id}.html",
                new { controller = "Ad", action = "Details", id = 0, clientName = @"[\w-]+", cate = @"[\w-]+" }, // Parameter defaults
                new string[] { "ChoViet.CMS.Controllers" } // Parameter defaults
            );
            routes.MapRoute(
            "Subject",
            "tin-rao-chuyen-de/{subjectName}-{id}",
            new { controller = "Subject", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
            "AdCreate",
            "Đăng tin rao vặt/",
            new { controller = "Ad", action = "Create", id = UrlParameter.Optional }
            );
            routes.MapRoute(
            "Introduction",
            "Giới thiệu về chợ 102.vn/",
            new { controller = "Home", action = "Introduction", id = UrlParameter.Optional }
            );
            routes.MapRoute(
            "Feedback",
            "Gửi phản hồi về chợ 102.vn/",
            new { controller = "Home", action = "Feedback", id = UrlParameter.Optional }
            );
            routes.MapRoute(
            "Contact",
            "Liên hệ với chợ 102.vn/",
            new { controller = "Home", action = "Contact", id = UrlParameter.Optional }
            );
            routes.MapRoute(
            "Regulations",
            "Các điều khoản, quy định tại chợ 102.vn/",
            new { controller = "Home", action = "Regulations", id = UrlParameter.Optional }
            );
            routes.MapRoute(
            "Postnews",
            "Các quy định đăng tin tại chợ 102.vn/",
            new { controller = "Home", action = "Postnews", id = UrlParameter.Optional }
            );
            routes.MapRoute(
                "foryou", // Router for controler FORYOU
                "foryou/{emailAdress}", // URL with parameters
                new { controller = "foryou", action = "Index", emailAdress = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = 0 } // Parameter defaults
            );
            routes.MapRoute(
                "DefaultCms", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "IndexCms", id = 0 } // Parameter defaults
            );

            routes.MapRoute(
                "DelObjectClass",
                "{controller}/{action}/{id}/{type}",
                new { controller = "Applications", action = "Del", id = 0, type = UrlParameter.Optional }
                );

            routes.MapRoute(
                "ListAdByUserId",
                "{controller}/{action}/{id}/{page}",
                new { controller = "Ad", action = "loadAdByUserId", id = 0, page = 0 }
                );

            routes.MapRoute(
                "Default_id_bool", // Route name
                "{controller}/{action}/{id}/{pubYN}", // URL with parameters
                new
                {
                    controller = "Home",
                    action = "Index",
                    id = 0,
                    pubYN = false
                } // Parameter defaults
            );
            routes.MapRoute(
                "loadAdByUserId", // Route name
                "{Ad}/{loadAdByUserId}/{id}/{adId}/{type}", // URL with parameters
                new
                {
                    controller = "Ad",
                    action = "loadAdByUserId",
                    id = 0,
                    adId = 0,
                    type = 0
                }
            );
            routes.MapRoute(
                "RewriteAdIndex",
                "{demanNam}/{cateName}/{cityName}/{adAtributeName}/{adAtributeValue}/{cusTypeId}",
                new { controller = "Ad", action = "Index", id = 0, demanNam = @"[\w-]+", cateName = @"[\w-]+", cityName = @"[\w-]+", adAtributeName = @"[\w-]+", adAtributeValue = @"[\w-]+", cusTypeId = 0 }, // Parameter defaults
                new string[] { "ChoViet.CMS.Controllers" } // Parameter defaults
            );
            routes.MapRoute(
                "ApplyChangeYN",
                "{controller}/{action}/ApChangeYN/{tableName}/{fieldName}/{id}/{pubYN}",
                new { controller = "Applications", action = "ApplyChangeYN", tableName = UrlParameter.Optional, fieldName = UrlParameter.Optional, id = UrlParameter.Optional, pubYN = UrlParameter.Optional }
                );
            
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);

            //RouteTable.Routes.RouteExistingFiles = true;
            //RegisterRoutes(RouteTable.Routes);
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

            Application["OnlineUsers"] = 0;
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            Application.Lock();
            CounterManager.Counter();
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
            Application.UnLock();
            Session["CurCountryID"] = "1";
        }

        protected void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate 
            // mode is set to InProc in the Web.config file. 
            // If session mode is set to StateServer or SQLServer, 
            // the event is not raised.
            Application.Lock();
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
            Application.UnLock();
        }
    }
}